package com.tian.service.impl;

import com.alibaba.fastjson.JSON;
import com.tian.entity.ChargeRecord;
import com.tian.entity.ChargeStationGun;
import com.tian.entity.ChargeStationInfo;
import com.tian.enums.ChargingStatusEnum;
import com.tian.enums.PayStatusEnum;
import com.tian.mapper.ChargeRecordMapper;
import com.tian.message.ChargingDataMessage;
import com.tian.service.ChargingDataService;
import com.tian.util.StationRedisPreKey;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * {@code @description:} 充电记录数据处理服务实现类
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-08-25 16:30
 * {@code @version:} 1.0
 */
@Service
public class ChargingDataServiceImpl implements ChargingDataService {
    @Resource
    private ChargeRecordMapper chargeRecordMapper;
    @Resource
    private RedissonClient redissonClient;

    @Override
    public Boolean doChargeData(ChargingDataMessage chargingDataMessage) {
        ChargeRecord chargerRecord = chargeRecordMapper.selectByByUserId(chargingDataMessage.getUserId());
        //更新充电实时数据
        if (chargerRecord != null) {
            chargerRecord.setQuantity(chargingDataMessage.getChargingCount());
            chargerRecord.setEndTime(chargingDataMessage.getChargingEndTime());
            chargerRecord.setStartTime(chargingDataMessage.getChargingStartTime());
            chargeRecordMapper.updateByPrimaryKey(chargerRecord);
            cacheChargingData(chargerRecord);
            return Boolean.TRUE;
        }
        RBucket<String> chargeStationGunBucket = redissonClient.getBucket(StationRedisPreKey.STATION_GUN_NO_PRE_KEY + chargingDataMessage.getGunNo());
        ChargeStationGun chargeStationGun = JSON.parseObject(chargeStationGunBucket.get(), ChargeStationGun.class);

        RBucket<String> chargeStationBucket = redissonClient.getBucket(StationRedisPreKey.STATION_PRE_KEY + chargeStationGun.getStationId());
        ChargeStationInfo chargeStationInfo = JSON.parseObject(chargeStationBucket.get(), ChargeStationInfo.class);
        //插入充电记录
        chargerRecord = new ChargeRecord();
        chargerRecord.setStationGunId(chargeStationGun.getId());
        chargerRecord.setUserId(chargingDataMessage.getUserId());
        chargerRecord.setPayStatus(PayStatusEnum.INIT.getPayStatus());
        chargerRecord.setPrice(chargeStationInfo.getPrice());
        chargerRecord.setStartTime(chargingDataMessage.getChargingStartTime());
        chargerRecord.setCreateTime(chargingDataMessage.getChargingEndTime());
        chargerRecord.setQuantity(chargingDataMessage.getChargingCount());
        chargerRecord.setChargeStatus(ChargingStatusEnum.CHARGING.getStatus());
        chargeRecordMapper.insert(chargerRecord);
        cacheChargingData(chargerRecord);
        return Boolean.TRUE;
    }

    /**
     * 缓存充电实时数据（缓存数据中多一个充电预估时间，因为这个预估时间没有必要留存到数据库）
     */
    private void cacheChargingData(ChargeRecord chargerRecord) {
        RBucket<String> chargingDataBucket = redissonClient.getBucket(StationRedisPreKey.CHARGING_DATA_LOG_PRE_KEY + chargerRecord.getId());
        chargingDataBucket.set(JSON.toJSONString(chargerRecord));
    }
}
